package com.ysd.lis.service.lab.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabBarCodeTypeMapper;
import com.ysd.lis.mapper.lab.LabRptVsBarcodeMapper;
import com.ysd.lis.request.BarCodeTypePageParam;
import com.ysd.lis.request.DeleteRptVsBarCodeParam;
import com.ysd.lis.service.lab.LabRptVsBarcodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2023-12-16
 */
@Service
public class LabRptVsBarcodeServiceImpl extends ServiceImpl<LabRptVsBarcodeMapper, LabRptVsBarcode> implements LabRptVsBarcodeService {

    @Autowired
    LabBarCodeTypeMapper labBarCodeTypeMapper;
    @Autowired
    LabRptVsBarcodeMapper labRptVsBarcodeMapper;

    /**
     * 获取报告单元列表
     * @param id
     * @return
     */
    @Override
    public Result findBarCodeListByRptId(String rptGroup) {

        Assert.hasText(rptGroup, "rptGroup must not be empty");
        MPJLambdaWrapper<LabBarCodeType> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(LabBarCodeType.class)
                .selectAs(LabRptVsBarcode::getId, LabBarCodeType::getVsBarCodeTypeId)
                .leftJoin(LabRptVsBarcode.class, LabRptVsBarcode::getBarcodeNo, LabBarCodeType::getBarcodeNo)
                .eq(LabRptVsBarcode::getRptGroup, rptGroup)
                .eq(LabBarCodeType::getDelFlag, 0)
                .eq(LabRptVsBarcode::getDelFlag, 0)
                .orderByAsc(LabRptVsBarcode::getSeq);
        List<LabBarCodeType> labBarCodeTypes = labBarCodeTypeMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", labBarCodeTypes);

    }

    /**
     * 删除对照
     * @param deleteRptVsBarCodeParam
     * @return
     */
    @Override
    public Result deleteRptVSBarCode(DeleteRptVsBarCodeParam deleteRptVsBarCodeParam) {
        int i = labRptVsBarcodeMapper.deleteById(deleteRptVsBarCodeParam.getId());
        if (i > 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }

    /**
     *获取分管类别
     * @param barCodeTypePageParam
     * @return
     */
    @Override
    public Result findBarCodeTypeListByRpt(BarCodeTypePageParam barCodeTypePageParam) {

        // Assert.hasText(id, "id must not be empty");
        MPJLambdaWrapper<LabBarCodeType> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(LabBarCodeType.class).eq(LabBarCodeType::getDelFlag, 0);
        if (StrUtil.isNotEmpty(barCodeTypePageParam.getKeyWords())) {
            mpjLambdaWrapper.and(wrapper -> wrapper.like(LabBarCodeType::getBarcodeNa, barCodeTypePageParam.getKeyWords()).or().like(LabBarCodeType::getBarcodeNo, barCodeTypePageParam.getKeyWords()));
        }
        //StringBuffer existsSql =new StringBuffer("SELECT 1 FROM t_check_standard_item TAS WHERE t_check_standard_type.ID = TAS.standard_type_id and tas.Del_Flag = 0  and TAS.equipment_id = '"+equipmentId+"'") ;
        StringBuffer existsSql =new StringBuffer("SELECT 1 FROM LAB_RPT_VS_BARCODE t2 WHERE t.BARCODE_NO = t2.BARCODE_NO AND t2.RPT_GROUP = '"+barCodeTypePageParam.getRptGroup()+"' ") ;

        mpjLambdaWrapper.notExists(existsSql.toString());
        List<LabBarCodeType> labBarCodeTypes = labBarCodeTypeMapper.selectList(mpjLambdaWrapper);
        return Result.succ(1, "查询成功", labBarCodeTypes);

    }

    /**
     * 添加对照
     * @param labRptVsBarcode
     * @return
     */
    @Override
    public Result addRptBarCode(LabRptVsBarcode labRptVsBarcode) {
        boolean save = this.save(labRptVsBarcode);
        if (save) {
            return Result.succ(1, "", null);
        } else {
            return Result.fail("添加失败！");
        }
    }


}
